package com.txd.mofa.fabu.aty;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseframework.util.Preferences;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.dialog.v2.WaitDialog;
import com.kongzue.takephoto.util.imagezip.CompressHelper;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.adapter.ShenheAdapter;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.bean.MeiyanBean;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.xiangce.ui.EasyPhotosActivity;
import com.wang.avi.AVLoadingIndicatorView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

@Layout(R.layout.aty_shenhe)
public class ShenheAty extends BaseAty {

    private ImageView iv,iv_queren,iv_fuhe,iv_beijing;
    private TextView tv_quxiao;
    private String filename;
    private String aa;
    private RecyclerView recy;
    private AVLoadingIndicatorView avi;
    private ShenheAdapter shenheAdapter;
    private Map<String, String> data;

    @Override
    public void initViews() {
        super.initViews();
        iv_beijing = findViewById(R.id.iv_beijing);
        iv_fuhe = findViewById(R.id.iv_fuhe);
        recy = findViewById(R.id.recy);
        avi = findViewById(R.id.avi);
        tv_quxiao = findViewById(R.id.tv_quxiao);
        iv = findViewById(R.id.iv);
        iv_queren = findViewById(R.id.iv_queren);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(me);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recy.setLayoutManager(linearLayoutManager);

        shenheAdapter = new ShenheAdapter(R.layout.item_shenhe);
        recy.setAdapter(shenheAdapter);
        recy.setOverScrollMode(View.OVER_SCROLL_NEVER);

        avi.show();
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

        WaitDialog.dismiss();

    }

    public static String encodeBase64File(String path) throws Exception {
        File  file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int)file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return Base64.encodeToString(buffer,Base64.DEFAULT);
    }


    private boolean flag = true;

    private String base64 = "";

    public static String beanToJson(Object bean) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(bean);
        System.out.println(jsonStr);
        return jsonStr;
    }


    private void http(File jpgFile) {

        FileInputStream inputFile = null;
        try {
            inputFile = new FileInputStream(jpgFile);
            byte[] buffer = new byte[(int)jpgFile.length()];
            inputFile.read(buffer);
            inputFile.close();
            base64 = Base64.encodeToString(buffer,Base64.DEFAULT);
        } catch (Exception e) {


        }


        WaitDialog.show(me,"图片检测中...");

        Log.e("ididiidid",Preferences.getInstance().getString(me,"user_id","user_id"));

        HttpRequest.POST(me, "http://api.magichairai.com:5003/api/face", new Parameter()
                .add("token","LTAI4FiwNU3MqGCFQGu5Xq7d")
                .add("user_id", Preferences.getInstance().getString(me,"user_id","user_id"))
                .add("img_b64",base64)
                .add("payload",beanToJson(new MeiyanBean("Android"))), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    iv_fuhe.setVisibility(View.VISIBLE);
//                    tv.setVisibility(View.VISIBLE);
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);

                    if (map.get("code").equals("1")){

                        data = JSONUtils.parseKeyAndValueToMap(map.get("data"));

                        avi.hide();
                        iv_beijing.setVisibility(View.GONE);
                        flag = true;
                        iv_queren.setVisibility(View.VISIBLE);
                        Glide.with(me).load(data.get("url")).dontAnimate().thumbnail(0.3f).into(iv);

                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(data.get("msg"));

                        shenheAdapter.setNewData(list);

                        WaitDialog.dismiss();

                    }else {
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        avi.hide();
                        iv_beijing.setVisibility(View.GONE);
                        flag = false;
                        Glide.with(me).load(filename).dontAnimate().thumbnail(0.3f).into(iv);
                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(data.get("msg"));

                        shenheAdapter.setNewData(list);

                        iv_queren.setVisibility(View.VISIBLE);
                        iv_queren.setImageResource(R.mipmap.chongpai);
                        iv_fuhe.setImageResource(R.mipmap.bufuhe);
//                        tv.setText("不符合");
                        WaitDialog.dismiss();
                    }
                }else {
                    avi.hide();
                    iv_fuhe.setVisibility(View.VISIBLE);
//                    tv.setVisibility(View.VISIBLE);
                    iv_queren.setVisibility(View.VISIBLE);
                    iv_beijing.setVisibility(View.GONE);
                    iv_queren.setImageResource(R.mipmap.chongpai);
                    iv_fuhe.setImageResource(R.mipmap.bufuhe);
//                    tv.setText("网络异常");
                    toast("网络异常");
                    WaitDialog.dismiss();
                }
            }
        });
//        HttpRequest.POST(me, beauty, new Parameter()
//                        .add("token",token)
//                .add("img_b64",base64)
//                .add("type","android")
//                , new ResponseListener() {
//            @Override
//            public void onResponse(String response, Exception error) {
//                if (error == null) {
//                    iv_fuhe.setVisibility(View.VISIBLE);
////                    tv.setVisibility(View.VISIBLE);
//                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
//
//                    if (map.get("code").equals("1")){
//
//                        data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
//
//                        avi.hide();
//                        iv_beijing.setVisibility(View.GONE);
//                        flag = true;
//                        iv_queren.setVisibility(View.VISIBLE);
//                        Glide.with(me).load(data.get("url")).dontAnimate().thumbnail(0.3f).into(iv);
//
//                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(data.get("msg"));
//
//                        shenheAdapter.setNewData(list);
//
//                        WaitDialog.dismiss();
//
//                    }else {
//                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
//                        avi.hide();
//                        iv_beijing.setVisibility(View.GONE);
//                        flag = false;
//                        Glide.with(me).load(filename).dontAnimate().thumbnail(0.3f).into(iv);
//                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(data.get("msg"));
//
//                        shenheAdapter.setNewData(list);
//
//                        iv_queren.setVisibility(View.VISIBLE);
//                        iv_queren.setImageResource(R.mipmap.chongpai);
//                        iv_fuhe.setImageResource(R.mipmap.bufuhe);
////                        tv.setText("不符合");
//                        WaitDialog.dismiss();
//                    }
//                }else {
//                    avi.hide();
//                    iv_fuhe.setVisibility(View.VISIBLE);
////                    tv.setVisibility(View.VISIBLE);
//                    iv_queren.setVisibility(View.VISIBLE);
//                    iv_beijing.setVisibility(View.GONE);
//                    iv_queren.setImageResource(R.mipmap.chongpai);
//                    iv_fuhe.setImageResource(R.mipmap.bufuhe);
////                    tv.setText("网络异常");
//                    toast("网络异常");
//                    WaitDialog.dismiss();
//                }
//            }
//        });
    }
    @Override
    public void initDatas(JumpParameter paramer) {

        aa = getIntent().getStringExtra("aa");
        if(!aa.equals("aa")){
            tv_quxiao.setText("重新拍照");
        }
        filename = getIntent().getStringExtra("filename");
        Glide.with(me).load(filename).dontAnimate().thumbnail(0.3f).into(iv);

        File newFile = CompressHelper.getDefault(this).compressToFile(new File(filename));

        http(newFile);

    }

    @Override
    public void setEvents() {

        iv_queren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (flag){
                    HttpRequest.POST(me, beautifulSuccess, new Parameter()
                            .add("token",token).add("url",data.get("url")), new ResponseListener() {
                        @Override
                        public void onResponse(String response, Exception error) {
                            if (error == null) {
                                Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                if (map.get("code").equals("1")){
                                    finish();
                                }

                            }else {
                                toast("网络异常");
                            }
                        }
                    });
                }else {
                    if (aa.equals("aa")){
                        jump(EasyPhotosActivity.class);
                    }else {
                        jump(PaizhaoAty1.class);
                    }
                    finish();
                }


            }
        });
        tv_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!aa.equals("aa")){
                    jump(PaizhaoAty1.class);
                    finish();
                }else {
                    finish();
                }
            }
        });
    }
}
